# *****************************************************************
# OVERVIEW ####
# *****************************************************************
# DESCRIPTIVE.R
# Are Rural Attitudes Just Republican?
# Descriptive Statistics
# Jennifer Lin and Kristin Lunz Trujillo
# Created On: 2023 01 27

# *** The following script generates descriptive statistics for
#   the paper and includes code on components that are in the 
#   main text and Appendix. ***

# ** Before running this file, ensure that you have ran
#   - Functions.R
#   - ANES20_CLEAN.R
#  or components of this code will result in an error. See 
#  00_GENERAL.R for more details **

# *****************************************************************
# DESCRIPTIVE STATISTICS IN MAIN PAPER ####
# *****************************************************************

## Figure 1 -- Distribution of Respondents ####

# In Figure 1, we computed the number of respondents who belong
#   in each of the party-place of residence categories.

# To do this, we first generate a survey-weighted count of the
#   number of participants in each category
Rural_Party <- ANES_Survey %>% 
  group_by(Rural_Party) %>% 
  survey_count() %>% 
  filter(!is.na(Rural_Party))

# Then, we generate a plot to reflect the number of respondents
#   plus the survey-weighted standard errors generated from above
ggplot(
  Rural_Party, aes(x = Rural_Party, y = n, fill = Rural_Party))+
  geom_bar(
    stat = 'identity', 
    position = position_dodge(), color = "black")+
  geom_errorbar(
    aes(ymin = n-1.96*n_se, ymax = n+1.96*n_se), 
    width = 0.1, size = 1)+
  scale_fill_manual(
    name = "Residence + Party",
    values = c(
      "Democrat-Rural" = "#6baed6",
      "Democrat-Urban" = "#08519c",
      "Republican-Rural" = "#fb6a4a",
      "Republican-Urban" = "#cb181d"
    )
  )+
  xlab("Residence + Party Classification")+
  ylab("Weighted Number of Respondents")+
  labs(
    title = "Distribution of ANES Respondents",
    subtitle = "By Place of Residence and Party Classification",
    caption = 
      "Data: American National Elections Stidues (ANES) 2020"
  )+
  theme_bw()+
  theme(
    plot.title    = element_text(
      hjust = 0.5, size = 20, colour="black", face = "bold"),
    plot.subtitle = element_text(
      hjust = 0.5, size = 16, colour="black"),
    legend.title  = element_text(
      hjust = 0.5, size = 14, colour="black", face = "bold"),
    plot.caption  = element_text(
      size = 10, colour="black"),
    axis.title    = element_text(
      size = 14, colour="black", face = "bold"),
    axis.text     = element_text(
      size = 12, colour="black", angle = 0, hjust = 0.5),
    legend.position = "none"
  )


## Figure 2 ####

# In Figure 2, we compute the average ratings for participants
#   in each of the party-place of residence categories. 

# To do this, we 
#   - Compute by party-place categorization
#   - Generate a survey weighted mean to represent the average
#     response value for each group
#   - Compute a 95% confidence interval around the mean
issue_avg <- ANES_Survey %>% 
  group_by(Rural_Party) %>% 
  summarise_at(vars(starts_with("I_")), svymNA) %>% 
  filter(!is.na(Rural_Party)) %>% 
  reshape2::melt(by = c(Rural_Party), na.rm = TRUE) %>%
  mutate(valtype = ifelse(grepl("_se", variable), "se","mean")) %>%
  mutate(variable = gsub("_se", "", variable)) %>%
  pivot_wider(
    names_from = "valtype",
    values_from = "value") %>%
  mutate(
    lwr = mean - 1.96*se,
    upr = mean + 1.96*se,
    Issue_Desc = case_when(
      variable == "I_ACA" ~ "Approve Affordable Care Act",
      variable == "I_banAR" ~ "Ban Assault Style Rifles",
      variable == "I_birthright" ~ "End Birthright Citizenship",
      variable == "I_bkgCheck" ~ 
        "Background Check for\n Gun Purchases",
      variable == "I_BorderWall" ~ "Build Wall on Southern Border",
      variable == "I_buyBackAR" ~ "Assault Rifle Buy Back",
      variable == "I_citizenship" ~ 
        "Provide a Path to Citizenship",
      variable == "I_deport" ~ 
        "Deporting Immigrants\n to Native Country",
      variable == "I_FamilyLeave" ~ "Provide Paid Family Leave",
      variable == "I_FelonVote" ~ "Allowing Felons to Vote",
      variable == "I_FreeTrade" ~ "Allowing Free Trade Agreements",
      variable == "I_incHCspend" ~ 
        "Increasing Spending\n to Cover Health Care",
      variable == "I_JAccess" ~ "Transparency for Journalists",
      variable == "I_Opioid" ~ 'Address Opioid Epidemic',
      variable == "I_reduceIneq" ~ "Reduce Income Inequality",
      variable == "I_refugees" ~ "Allow Refugees to Come to US",
      variable == "I_RegGHG" ~ 
        "Regulate Greenhouse\n Gas Emissions",
      variable == "I_ReqIDVote" ~ "Require ID to Vote",
      variable == "I_reqVax" ~ "Require Vaccinations",
      variable == "I_SPChild" ~ 
        "Separating Children from\n Parents at Border",
      variable == "I_TransMil" ~ 
        "Allow Transgender People\n to Serve in Military",
      variable == "I_UBI" ~ "Provide Citizens 12K A Year"
    )
  )

# We can generate the plot using the information above.
ggplot(
  issue_avg, 
  aes(x = Issue_Desc, y = mean, color = Rural_Party))+
  geom_pointrange(
    aes(ymin = lwr, ymax = upr),
    position = position_dodge(.7))+
  scale_color_manual(
    name = "Residence + Party",
    values = c(
      "Democrat-Rural" = "#6baed6",
      "Democrat-Urban" = "#08519c",
      "Republican-Rural" = "#fb6a4a",
      "Republican-Urban" = "#cb181d"
    )
  )+
  xlab("Issues")+
  ylab("Mean")+
  labs(
    title = "Posiions on Core Issues",
    subtitle = "By Party and Place of Residence",
    caption = "Data: American National Elections Studies 2020"
  )+
  coord_flip()+
  guides(color = guide_legend(ncol = 2))+
  theme_bw()+
  theme(
    plot.title    = element_text(
      hjust = 0.5, size = 20, colour="black", face = "bold"),
    plot.subtitle = element_text(
      hjust = 0.5, size = 16, colour="black"),
    legend.title  = element_text(
      hjust = 0.5, size = 14, colour="black", face = "bold"),
    plot.caption  = element_text(
      size = 10, colour="black"),
    axis.title    = element_text(
      size = 14, colour="black", face = "bold"),
    axis.text     = element_text(
      size = 12, colour="black", angle = 0, hjust = 0.5),
    legend.position = "bottom"
  )

# For ease of use in the Chi-Squared tests below, we pull a vector
#   representing the descriptions for the issues for use later on
issues <- unique(issue_avg$Issue_Desc)

# *****************************************************************
# CHI-SQUARED TESTS IN SUPPLEMENTAL APPENDIX ####
# *****************************************************************

## Chi-Squared tests in Supplemental Appendix C ####

# To generate a Chi-Squared test for the issue positions by our
#   party-place of residence variable, we 
#   - Generate a second survey object so that the variables for the
#     issue positions are treated categorically rather than
#     continuously. 
#   - From here, we can compute the Chi-Squared statistic for each
#     issue position.
ANES_chi <- ANES_Issues %>% 
  filter(!is.na(Rural_Party)) %>% 
  mutate_at(vars(starts_with("I_")), as.character) %>% 
  srvyr::as_survey(weight = V200010a)

# We extract a data frame that consists of the variables that 
#   measure issue attitudes, analyzed above.
issue_vars <- ANES_Issues %>% 
  select(starts_with("I_")) 

# We compute a chi-squared statistic for each of the variables
#   by iterating through the variables for effeciency.
chi_issues <- chi_squ_svy(issue_vars, 1:22, design = ANES_chi)

# Create empty vector to store Chi-Squared estimate
x_sq <- c()
# Create empty vector to store p-value
x_p <- c()

# Use the following for loop to parse the chi-squared statistic
#   and p value from each of the analyses stored in the resulting
#   list from above.
for (i in 1:length(chi_issues)) {
  x_sq[i] <- chi_issues[[i]]$statistic
  x_p[i] <- chi_issues[[i]]$p.value
}

# From here, we can export the data as one concice table, as seen
#   in Supplemental Appendix C.
chi_sq_output <- data.frame(
  Issues = issues, 
  `X-Squared` = x_sq,
  `p.value` = x_p
) %>% 
  mutate(
    signif = case_when(
      p.value <= 0.001 ~ "***",
      p.value > 0.001 & p.value <= 0.05 ~ "**",
      p.value > 0.05 & p.value <= 0.1 ~ "*",
      TRUE ~ ""
    ),
    `Residence and Party` = paste0(
      round(`X.Squared`, 2), signif
    )
  ) %>% 
  select(Issues, `Residence and Party`)

